<?php

namespace app\common\controller;

use think\Controller;
use think\Db;
use think\Exception;
use think\Session;

class Base extends Controller
{
    protected $system, $admin, $user, $seller;

    public function _initialize()
    {
        parent::_initialize();
        $config = cache('db_config_data');
        if (!$config) {
            $config = action('common/Config/lists');
            cache('db_config_data', $config);
        }
        config($config);
        $jump_module = ['ajax'];
        if (!in_array(request()->module(), $jump_module)) {
            $this->siteCheck();
        }

    }

    public function siteCheck()
    {

        if (config('web_site_close') == 0  && session('manageInfo')['id'] != 1 && request()->module() !== 'system') {
            $this->error('站点已经关闭，请稍后访问~');
        }


        // 后台未登录时去登录
        if (request()->module() == 'system' && request()->controller() != 'Login' && !session('manageInfo')['id']) {
            $this->redirect('system/login/index');
        }

        // 获取已登录管理员信息
        if (session('admin_id')) {
            $this->admin = db('admin')->find(session('admin_id'));
            $this->assign("_admin", $this->admin);
        }

        // 获取已登录会员信息
        if (session('user')) {
            $this->user =  db('user t')
            ->join('level_think lg', 't.user_type = lg.id', 'left')
            ->field('t.*,lg.title as lg_title')
            ->where('t.id',session('user.id'))
            ->find();;
            $this->assign("_user", $this->user);

        }
    }


    // 获取当前角色菜单信息
    public function PubGetMenu($RoleId)
    {
        $Role = db('admin_role')->where(['id' => $RoleId])->find();
        $where['role_id'] = $RoleId;
        $where['parent_id'] = $Role['RoleType'];
        $where['is_del'] = 0; // 获取不停用的菜单栏目
        $where['IsShow'] = 1; // 获取不停用的菜单栏目
        $NavList = db('admin_role_nav')->where($where)->order('Sort')->select();
        foreach ($NavList as $key => &$value) {
            $son['parent_id'] = $value['menu_id'];
            $son['role_id'] = $RoleId;
            $son['is_del'] = 0; // 获取不停用的菜单栏目
            $son['IsShow'] = 1; // 获取不停用的菜单栏目
            $value['son'] = db('admin_role_nav')->where($son)->order('Sort')->select();
        }
        return $NavList;
    }
    /**
     * 获取当前角色菜单信息
     * @param $RoleId
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getMenu($RoleId)
    {
        $Role = db('admin_role')->where(['id' => $RoleId])->find();
        $where['role_id'] = $RoleId;
        $where['parent_id'] = $Role['RoleType'];
        $where['is_del'] = 0;//获取不停用的菜单栏目
        $NavList = db('admin_role_nav')->where($where)->order('Sort')->select();
        // $where['IsShow'] = 1;
        foreach ($NavList as $key => &$value) {
            $son = [
                'parent_id' => $value['menu_id'],
                'role_id' => $RoleId,
                'is_del' => '0',
            ];
            $value['son'] = db('admin_role_nav')->where($son)->order('Sort')->select();
        }
        return $NavList;
    }

    /**
     * @param $table
     * @param $del_where
     * @param array $data
     * @param Closure $func
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * 处理子表数据
     */
    function createChildTableDatas($table, $del_where, array $data, \Closure $func)
    {
        if (!empty($data)) {
            Db::startTrans();
            try {
                //删除旧图
                db($table)->where($del_where)->delete();

                //上传新图
                $datas = [];
                foreach ($data as $k => $v) {
                    $datas[] = $func($v);
                }
                db($table)->insertAll($datas);
                Db::commit();
                return return_message('1', '保存成功');
            } catch (Exception $e) {
                Db::rollback();
                return return_message('0', '保存失败');
            }
        } else {
            return return_message('1', '保存成功');
        }
    }
    function editState($table, $id, $key, $val, $primary_key = 'id', $update_key = 'update_time')
    {
        $value = $val == 'true' ? '1' : '0';
        $res = db($table)->where([$primary_key => ['in', $id]])->update([$key => $value, $update_key => time()]);
        if ($res) {
            return return_message('1', '成功');
        } else {
            return return_message('0', '失败');
        }
    }

}